package us.icitap.utils;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class AppMail {

	private String recipients[];
	private String subject; 
	private String message; 
	private String sender; 
	private AppDataSource appDataSource;
	
	
	public AppMail(String[] recipients, String subject, String message,
			String sender, AppDataSource appDataSource) {
		super();
		this.recipients = recipients;
		this.subject = subject;
		this.message = message;
		this.sender = sender;
		this.appDataSource = appDataSource;
	}
	
	public AppMail(String[] recipients, String subject,
			String sender, AppDataSource appDataSource) {
		super();
		this.recipients = recipients;
		this.subject = subject;
		this.message = "Ky eshte nje email i gjeneruar automatikisht nga sistemi.";
		this.sender = sender;
		this.appDataSource = appDataSource;
	}
	
	public AppMail(String recipients, String subject,
			String sender, AppDataSource appDataSource) {
		super();
		this.recipients = new String[]{recipients};
		this.subject = subject;
		this.message = "Ky eshte nje email i gjeneruar automatikisht nga sistemi.";
		this.sender = sender;
		this.appDataSource = appDataSource;
	}
	
	public void setMessage(String message){
		this.message = message;
	}
	public void postMail() throws MessagingException{
		System.out.println("AppMail.postMail()");
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
	 	// Create some properties and get the default session
	 	Session session = Session.getInstance(props,
	 	
	 	new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("klaudetamertiku@gmail.com", "1112009roissokoli");
			}
		  });
	 	
	 	try {
	 		boolean  debug = false;
		 	session.setDebug(debug);
		 	//Create a message
		 	Message msg = new MimeMessage(session);
		 	//set the from and to addrese
		 	InternetAddress addressSender = new InternetAddress(sender);
		 	msg.setFrom(addressSender);
		 	InternetAddress[] addressTo = new InternetAddress[recipients.length];
		 	for (int i = 0; i < recipients.length; i++){
		 		addressTo[i] = new InternetAddress(recipients[i]);
		 	}
		 	msg.setRecipients(Message.RecipientType.TO, addressTo);
		 	// Setting the Subject and Content Type
		 	msg.setSubject(subject);


		 	 // create the message part 
		    MimeBodyPart messageBodyPart = 
		      new MimeBodyPart();

		    //fill message
		    messageBodyPart.setText(message);

		    Multipart multipart = new MimeMultipart();
		    multipart.addBodyPart(messageBodyPart);

		    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.HH.mm");
		    System.out.println("tytyty" + appDataSource);
		    if(appDataSource != null){
		    	// Part two is attachment
			    messageBodyPart = new MimeBodyPart();
			   System.out.println(appDataSource.getInputStream());
			    messageBodyPart.setDataHandler(new DataHandler(appDataSource));
			    messageBodyPart.setFileName(appDataSource.getName());
			    multipart.addBodyPart(messageBodyPart);
		    	
		    }
		    
		    // Put parts in message
		    msg.setContent(multipart);
		    
		    
		 	Transport.send(msg);
		 	System.out.println("AppMail.postMail()");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
